package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.CodeConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjCodeLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.vo.CodeLogListVo;
import com.zb.vo.CodeLogSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 编码日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-24
 */
@Service
public class ZjCodeLogServiceImpl extends ServiceImpl<ZjCodeLogMapper, ZjCodeLog> implements ZjCodeLogService {
    @Resource
    private ZjCodeLogMapper codeLogMapper;
    @Resource
    private ZjCodeLog02Service codeLog02Service;
    @Resource
    private ZjCodeLog03Service codeLog03Service;
    @Resource
    private ZjCodeLog04Service codeLog04Service;
    @Resource
    private ZjCodeLog05Service codeLog05Service;

    // 根据编码ID查询编码操作记录列表
    @Override
    public Map<String, Object> getList(Long codeId, CodeLogSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<CodeLogListVo> voList = codeLogMapper.getList(codeId, selectVo);
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), codeLogMapper.getListCount(codeId, selectVo), voList);
    }

    // 根据编码ID删除
    @Override
    public void deleteByCodeIds(List<Long> codeIds) {
        if (null == codeIds || codeIds.isEmpty()) {
            return;
        }
        codeLogMapper.deleteByCodeIds(codeIds);
    }

    // 保存编码操作日志
    @Override
    public void save(AccounLoginInfo loginInfo, int codeLogType, Long codeId, String name, String content) {
        ZjCodeLog codeLog = new ZjCodeLog();
        codeLog.setId(IdWorker.getId());
        codeLog.setCustAccId(loginInfo.getCustAccId());
        codeLog.setType(codeLogType);
        codeLog.setName(name);
        codeLog.setContent(content);
        codeLog.setCreateEmpId(loginInfo.getId());
        codeLog.setCreateEmp(loginInfo.getName());
        this.save(codeLog);
    }

    // 保存编码操作日志
    @Override
    public void save(Integer tableType, ZjCodeLog codeLog) {
        if (tableType == 0) {
            this.save(codeLog);
            return;
        }
        if (tableType == 1) {
            codeLog02Service.save(new ZjCodeLog02(codeLog));
            return;
        }
        if (tableType == 2) {
            codeLog03Service.save(new ZjCodeLog03(codeLog));
            return;
        }
        if (tableType == 3) {
            codeLog04Service.save(new ZjCodeLog04(codeLog));
            return;
        }
        if (tableType == 4) {
            codeLog05Service.save(new ZjCodeLog05(codeLog));
        }

    }

    // 分批保存编码日志
    @Override
    public void saveInBatch(List<ZjCodeLog> codeLogList) {
        if (CollectionUtil.isEmpty(codeLogList)) {
            return;
        }
        List<ZjCodeLog> logs = new ArrayList<>();
        List<ZjCodeLog02> log02s = new ArrayList<>();
        List<ZjCodeLog03> log03s = new ArrayList<>();
        List<ZjCodeLog04> log04s = new ArrayList<>();
        List<ZjCodeLog05> log05s = new ArrayList<>();
        for (ZjCodeLog log : codeLogList) {
            switch (CodeConstant.tableType(log.getCodeId())) {
                case 0:
                    logs.add(log);
                    continue;
                case 1:
                    log02s.add(new ZjCodeLog02(log));
                    continue;
                case 2:
                    log03s.add(new ZjCodeLog03(log));
                    continue;
                case 3:
                    log04s.add(new ZjCodeLog04(log));
                    continue;
                case 4:
                    log05s.add(new ZjCodeLog05(log));
            }
        }
        if (CollectionUtil.isNotEmpty(logs)) this.saveBatch(logs);
        if (CollectionUtil.isNotEmpty(log02s)) codeLog02Service.saveBatch(log02s);
        if (CollectionUtil.isNotEmpty(log03s)) codeLog03Service.saveBatch(log03s);
        if (CollectionUtil.isNotEmpty(log04s)) codeLog04Service.saveBatch(log04s);
        if (CollectionUtil.isNotEmpty(log05s)) codeLog05Service.saveBatch(log05s);
    }
}
